﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using DAQ.ModbusModule;

namespace DAQ.Config
{
    public class DaqOptionBuilder
    {
        string _path = "";

        public DaqOptionBuilder(string path = "Config")
        {
            _path = path;
        }

        public DaqOption Build()
        {
            var str = File.ReadAllText(_path + "\\Config.json");
            var option = JsonSerializer.Deserialize<DaqOption>(str);

            List<RegisterPoint> points = new List<RegisterPoint>();
            var lines = File.ReadAllLines(_path + "\\AllPoint.csv").ToList();
            lines.RemoveAt(0);
            foreach (var line in lines)
            {
                var values = line.Split(',');
                var point = new RegisterPoint
                {
                    UID = values[0],
                    Name = values[1],
                    Type = Type.GetType("System." + values[2]),
                    RegisterType = values[3],
                    Address = int.Parse(values[4]),
                    Length = int.Parse(values[5]),
                    DeviceId = values[6]
                };
                option.DeviceLinks.FirstOrDefault(x => x.UID == point.DeviceId)?.Points.Add(point);
            }

            return option;
        }
    }
}
